<template>
  <div>
    <el-dialog :title="'办理结果配置>申请'" :visible.sync="Configuration" width="1250px" :append-to-body="true" :close-on-click-modal="false">
      <el-form ref="form" :rules="rules" :model="assessInfoData">
        <el-table class="flowTable" stripe :data="assessInfoData" align="center">
          <el-table-column label="序号" type="index" width="75" align="center" />
          <el-table-column label="办理结果" prop="resultName" width="230px">
            <template slot-scope="scope">
              <el-select placeholder="请选择办理结果" v-model="scope.row.resultName">
                <el-option v-for="(item,index) in resultName" :key="index" :label="item.label" :value="item.label" />
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="指定下一步办理结果" prop="nextResultName" width="230px">
            <template slot-scope="scope">
              <el-select placeholder="请选择" v-model="scope.row.nextResultName">
                <el-option label="无" value />
                <el-option v-for="(item,index) in nextResultName" :key="index" :label="item.label" :value="item.label" />
              </el-select>
            </template>
          </el-table-column>
          <el-table-column prop="isShow" label="是否显示">
            <template slot-scope="scope">
              <el-select placeholder="请选择" v-model="scope.row.isShow">
                <el-option v-for="(item,index) in isShow" :key="index" :label="item.label" :value="item.value" />
              </el-select>
            </template>
          </el-table-column>
          <el-table-column prop="assigneeType" label="用户分配类型">
            <template slot-scope="scope">
              <el-select placeholder="请选择" v-model="scope.row.assigneeType" @change="assigneeTypeChange(scope)">
                <el-option label="无" value="-1"></el-option>
                <el-option v-for="(item,index) in assigneeType" :key="index" :label="item.label" :value="item.value" />
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="选择办理结果指定对应下一环节办理人" prop="assignee" width="200px">
            <template slot-scope="scope">
              <span @click="addBlr(scope)">
                <el-input v-model="scope.row.assignee" readonly="readonly" />
              </span>
            </template>
          </el-table-column>
          <el-table-column label="是否使用成员" prop="isUserPerson">
            <template slot-scope="scope">
              <el-select placeholder="请选择" v-model="scope.row.isUserPerson">
                <el-option v-for="item in isUserPerson" :key="item.value" :label="item.label" :value="item.value" />
              </el-select>
            </template>
          </el-table-column>
          <el-table-column :render-header="renderHeader">
            <template slot-scope="scope">
              <el-button icon="el-icon-delete" type="text" @click="deleteRow(scope)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer">
        <el-button @click="returns()">取 消</el-button>
        <el-button type="primary" v-throttle="[save]">保 存</el-button>
      </div>

      <flow-department v-on:loadsuc="Userload" ref="flowDepartment" />
      <flow-role v-on:loadsuc="Userload" ref="flowRole" />
      <flow-user v-on:loadsuc="Userload" ref="flowUser" />
    </el-dialog>
  </div>
</template>

<script>
import axios from 'axios'
import utils from '~/utils/index'
import dbl from '@/utils/dbl'
import flowUser from './flow-user'
import flowRole from './flow-role'
import flowDepartment from './flow-department'
import { delConfiguration } from '@/views/wy-flow/api/actreprocdef'

export default {
  name: "flow-configuration",
  components: { flowUser, flowDepartment, flowRole },
  data () {
    return {
      Configuration: false,
      assessInfoData: [],
      taskConfId: '',
      resultName: [],
      nextResultName: [],
      isShow: [
        { value: 1, label: "是" },
        { value: 0, label: "否" },
      ],
      assigneeType: [
      ],
      isUserPerson: [
      ],
      rules: {
        resultName: { required: true, message: '请选择办理结果', trigger: 'blur' },
      },
    }
  },
  methods: {
    save () {
      this.$refs.form.validate(valid => {
        if (valid) {
          const loading = this.$loading({
            lock: true,
            text: '数据处理中'
          });
          for (var i = 0; i < this.assessInfoData.length; i++) {
            if (this.assessInfoData[i].assignee == "请点击选择") {
              this.assessInfoData[i].assignee = ""
              this.assessInfoData[i].assigneeType = ""
            }
            this.assessInfoData[i].resultCode = ""
            this.assessInfoData[i].nextResultCode = ""
          }
          var thta = this
          const config = {
            headers: { 'Content-Type': 'application/json' }
          }
          axios.post("/api/flowTaskResult/doUpdateTaskResult", {
            taskConfId: this.taskConfId,
            taskResultListVo: this.assessInfoData
          }, config).then(response => {
            if (response.data.msg == null) {
              this.$message.success("添加成功")
            } else {
              this.$message.error(response.data.msg)
            }
            loading.close()
            thta.getList(thta.taskConfId)
          }).catch(e => {
            loading.close()
            this.$message.error('添加失败')

          })
        }
      })
    },
    renderHeader (h, params) {
      let a = [h('el-button', {
        props: {
          size: "mini",
          type: "primary"
        },
        on: {
          click: () => {
            this.addsome();
          }
        }
      }, '增加')];
      return h('div', a);
    },
    addsome () {
      this.assessInfoData.push({
        assigneeType: "-1",
        nextResultName: "无",
        resultName: this.resultName[0].label,
        isShow: 1,
        isUserPerson: "0",
        assignee: '请点击选择'
      })
    },
    deleteRow (data) {
      var thta = this;
      this.$confirm('是否确认删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function () {
        thta.assessInfoData.splice(data.$index, 1)
        if (data.row.taskResultId != '' && data.row.taskResultId != null) {
          return delConfiguration(data.row.taskResultId)
        }
      }).then(data => {
        this.$message.success('删除成功')
        // thta.getList(this.taskConfId)
      })
    },
    Userload (data) { // user 子组件返回值
      // 接收到的参数在data中
      if (data.data == "" || data.data == null) {
        this.assessInfoData[data.index].assignee = "请点击选择"
        this.assessInfoData[data.index].assigneeId = ""
      } else {
        this.assessInfoData[data.index].assignee = data.data
        this.assessInfoData[data.index].assigneeId = data.userId
      }
    },
    addBlr (data) {
      switch (data.row.assigneeType) {
        case "1":
          this.$refs.flowUser.show(data.$index)
          break;
        case "2":
          this.$refs.flowRole.show(data.$index)
          break
        case "3":
          this.$refs.flowDepartment.show(data.$index)
          break
        default:
          this.$message.error('请选择分配类型')
          break
      }
    },
    assigneeTypeChange (data) {
      switch (data.row.assigneeType) {
        case "1":
          break;
        case "2":
          break
        case "3":
          break
        default:
          this.assessInfoData[data.$index].assignee = "请点击选择"
          this.assessInfoData[data.$index].assigneeId = ""
          this.assessInfoData[data.$index].isUserPerson = ""
          break
      }
    },
    show (data) {
      this.Configuration = true
      this.taskConfId = data
      this.getList(data)
      this.resultName = utils.getDict('busi_task_result')
      this.nextResultName = utils.getDict('busi_task_result')
      this.assigneeType = utils.getDict('assignee_type')
      this.isUserPerson = utils.getDict('yesorno')
    },
    getList (data) {
      this.assessInfoData = []
      const config = {
        headers: { 'Authorization': dbl.get('token'), }
      }
      axios.get("/api/flowTaskResult/toResultList/" + this.taskConfId, config).then(response => {
        var k = response.data.data.resultList;
        for (var i = 0; i < k.length; i++) {
          var datas = {};
          datas.resultName = k[i].resultName
          datas.nextResultName = k[i].nextResultName
          datas.isShow = k[i].isShow
          datas.isUserPerson = k[i].isUserPerson
          if (datas.isUserPerson == '' || datas.isUserPerson == null) {
            datas.isUserPerson = "0"
          }
          datas.nextResultCode = k[i].nextResultCode
          datas.taskAssigneeId = k[i].assigneeId
          datas.resultCode = k[i].resultCode
          datas.taskResultId = k[i].taskResultId
          datas.assignee = k[i].assignee
          if (datas.assignee == '' || datas.assignee == null) {
            datas.assignee = "请点击选择"
          }
          datas.assigneeId = k[i].assigneeId
          datas.assigneeType = k[i].assigneeType
          if (datas.assigneeType == '' || datas.assigneeType == null) {
            datas.assigneeType = "-1"
          }
          this.assessInfoData.push(datas)
        }
      }).catch(e => {
      })
    },
    returns () {
      this.Configuration = false
      this.assessInfoData = []
    }
  }
}
</script>

<style scoped lang='scss'>
@include formWrap(4);
</style>

